﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using InterfaceXAML.Enum;
using InterfaceXAML.ModelBinding;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace InterfaceXAML.CalendarPages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class WeekCalendarPage : Page
    {
        public WeekCalendarPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            InitPage();
        }

        private async void InitPage()
        {
            var month = Utilities.Utilities.CurrentMonth;
            var year = Utilities.Utilities.CurrentYear;

            currentMonth.Text = ((MonthEnum)month).ToString();
            currentYear.Text = year.ToString();

            var ligne = 1;

            for (var i = 1; i < 7; i++ )
            {
                for(var j = 0; j<7; j++)
                {
                    ((Button)this.FindName("Button_" + i + "_" + j)).Visibility = Visibility.Collapsed;
                }
            }

            var reponse = await App.ExaPlanningServiceClient.GetCalendarMonthAsync(year, month);

            for (var i = 1; i < DateTime.DaysInMonth(year, month) + 1; i++)
            {
                var now = new DateTime(year, month, i);
                var dayOfWeek = Utilities.Utilities.GetRealDayOfWeek((int)now.DayOfWeek);

                ((TextBlock)this.FindName("Date_" + ligne + "_" + dayOfWeek)).Text = i.ToString();
                ((Button)this.FindName("Button_" + ligne + "_" + dayOfWeek)).Visibility = Visibility.Visible;

                ((ListView)FindName("item_" + ligne + "_" + dayOfWeek)).ItemsSource = reponse.CalendarMonthProjectSet[i - 1].ProjectSet;

                if (dayOfWeek == 6)
                    ligne++;
            }
            UpdateLayout();
        }

        private void BackButtonClick(object sender, RoutedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
            }
        }

        private void PrevMonthButtonClick(object sender, RoutedEventArgs e)
        {
            if(Utilities.Utilities.CurrentMonth == 1)
            {
                Utilities.Utilities.CurrentMonth = 12;
                Utilities.Utilities.CurrentYear--;
            }
            else
            {
                Utilities.Utilities.CurrentMonth --;
            }
            
            InitPage();
        }

        private void NextMonthButtonClick(object sender, RoutedEventArgs e)
        {
            if (Utilities.Utilities.CurrentMonth == 12)
            {
                Utilities.Utilities.CurrentMonth = 1;
                Utilities.Utilities.CurrentYear ++;
            }
            else
            {
                Utilities.Utilities.CurrentMonth ++;
            }
            InitPage();
        }

        private void DayClick(object sender, RoutedEventArgs e)
        {
            var indice = ((Button) e.OriginalSource).Name.Substring(6);
            Utilities.Utilities.CurrentDay = Convert.ToInt32(((TextBlock) FindName("Date" + indice)).Text);
            Frame.Navigate(typeof(WeekDetailPage));
        }
    }
}
